var url = "../Database/pse?providerName="

function refreshJson() {
    var json = getJson($("#providerSettings > .propertyInput"));
    $("#providerSettingsJson").val(json);
}

function getJson(elements: JQuery) {
    var json = "{";
    elements.each((i, e) => {
        if (i != 0) json += ",";
        var type = e.getAttribute("data-value-type");
        var name = e.getAttribute("data-propertyName");
        json += "\"" + name + "\":";
        if (type == "System.String") {
            json += "\"" +  (<string>$(e).children("input").val()).replace("\"", "\\\"") + "\"";
        }
    });
    json += "}"
    return json;
}

$(() => {
    $("#providerName").change(e => {
        var u = url + encodeURIComponent($("#providerName").val())
        $.ajax({
            url: u,
            success: (data) => {
                $("#providerSettings").html(data).ready(() => {
                    $("#providerSettings input").change(e => refreshJson());
                    refreshJson();
                });
            },
            error: (e) => {
                alert("error loading");
            }
        });
    }); 
})